package com.atguigu.tingshu.live.api;

import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.live.service.LiveRoomService;
import com.atguigu.tingshu.model.live.LiveRoom;
import com.atguigu.tingshu.vo.live.LiveRoomVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import org.bouncycastle.asn1.dvcs.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("api/live/liveRoom")
@SuppressWarnings({"unchecked", "rawtypes"})
public class LiveRoomApiController {

	@Autowired
	private LiveRoomService liveRoomService;

	@GetMapping("getCurrentLive")
	@GuiGuLogin
	public Result<LiveRoom> getCurrentLive(){
	LiveRoom liveRoom = 	this.liveRoomService.getOne(new LambdaQueryWrapper<LiveRoom>()
				.eq(LiveRoom::getUserId, AuthContextHolder.getUserId()).gt(LiveRoom::getExpireTime,new Date()).eq(LiveRoom::getStatus,1));
	return Result.ok(liveRoom);
	}
	@Operation(summary = "保存直播间")
	@PostMapping("saveLiveRoom")
	@GuiGuLogin
	public Result<LiveRoom> saveLiveRoom(@RequestBody @Validated LiveRoomVo liveRoomVo){
		LiveRoom liveRoom = this.liveRoomService.saveLiveRoom(liveRoomVo);
		return Result.ok(liveRoom);
	}


	@Operation(summary = "根据id查询直播间")
	@GuiGuLogin
	@GetMapping("getById/{roomId}")
	public Result<LiveRoom> getById(@PathVariable("roomId")Long roomId){
		LiveRoom liveRoom = this.liveRoomService.getById(roomId);
		return Result.ok(liveRoom);
	}

	@Operation(summary = "获取当前直播列表")
	@GetMapping("findLiveList")
	public Result<List<LiveRoom>>  findLiveList() {

		List<LiveRoom> liveRooms = this.liveRoomService.list(new LambdaQueryWrapper<LiveRoom>()
				.gt(LiveRoom::getExpireTime, new Date()));
		return Result.ok(liveRooms);
	}
}

